<?php
namespace app\admin\model;
use think\Model;
use think\Db;

class GoodsModel extends Model{

    public function index($goods_name){
        $where = " AND goods_name like '%$goods_name%'";
        if (empty($where)) {
            $rows = Db::table('goods')->where("is_del=0")->order('create_time desc')->paginate(7);
            return $rows;
        } else {
            $rows= Db::table('goods')->where("is_del=0 $where")->order('create_time desc')->paginate(7);
            return $rows;
        }
    }

    // 删除
    public function getRemove($data){
        $row = Db::table('goods')->update($data);
        return $row;
    }

    // 修改
    public function getUpdate($id){
        $res = Db::table('goods')->where(array('id'=>$id))->find();
        return $res;
    }

    // 增加
    public function getInsert($data){
        $row = Db::table('goods')->insert($data);
        return $row;
    }

    // 三级联动获取数据第一条数据
    public function getrows(){
        $rows = Db::table('goods_type')->where('parent_id',0)->select();
        return $rows;
    }

    // 获取第二条数据

    public function getSecond($id){
        $rows = Db::table('goods_type')->where(array('parent_id'=>$id))->select();
        return $rows;
    }

    // 批量删除数据
    public function delAll($id){
        $res = Db::table('goods')->where('id',$id)->update(['is_del'=>1]);
        return $res;
    }

    // 精品设置

    public function getbest($id){
        $res = Db::table('goods')->where('id',$id)->update(['is_best'=>1]);
//        echo $this->getLastSql();
//        exit;
        return $res;
    }
    // 取消精品
    public function getCancelbest($id){
        $res = Db::table('goods')->where('id',$id)->update(['is_best'=>0]);
        return $res;
    }

    // 今日必抢
    public function getActive($id){
        $res = Db::table('goods')->where('id',$id)->update(['is_active'=>1]);
        return $res;
    }
    // 取消必抢
    public function getcancelActice($id){
        $res = Db::table('goods')->where('id',$id)->update(['is_active'=>0]);
        return $res;
    }
    //主题推荐
    public function getPush($id){
        $res = Db::table('goods')->where('id',$id)->update(['is_push'=>1]);
        return $res;
    }
    // 取消主题
    public function getcancelRecom($id){
        $res = Db::table('goods')->where('id',$id)->update(['is_push'=>0]);
        return $res;
    }

    // 增加库存
    public function plusStock($data){
        $id =$data['id'];
        $res = Db::table('goods')->where('id',$id)->find();
        $goods_stock = $res['goods_stock'] + $data['initial_stock'];
        $initial_stock = $res['initial_stock'] + $data['initial_stock'];
        $datas = [
            'goods_stock'=>$goods_stock,
            'initial_stock'=>$initial_stock
        ];
        $ress = Db::table('goods')->where('id',$id)->update($datas);
        return $ress;
    }

    //减少库存
    public function reduceStock($data){
        $id =$data['id'];
        $res = Db::table('goods')->where('id',$id)->find();
        $goods_stock = $res['goods_stock'] - $data['initial_stock'];
        $initial_stock = $res['initial_stock'] - $data['initial_stock'];
        $datas = [
            'goods_stock'=>$goods_stock,
            'initial_stock'=>$initial_stock
        ];
        $ress = Db::table('goods')->where('id',$id)->update($datas);
        return $ress;
    }

}